package com.xcdh.trading.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcdh.trading.entity.StrongSector;
import com.xcdh.trading.entity.ZtStock;
import com.xcdh.trading.mapper.StrongSectorMapper;
import com.xcdh.trading.response.TradingRes;
import com.xcdh.trading.service.StrongSectorService;
import com.xcdh.trading.util.TuShareApi;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 强势板块服务实现类，继承 ServiceImpl 实现具体业务逻辑
 */
@Service
public class StrongSectorServiceImpl extends ServiceImpl<StrongSectorMapper, StrongSector> implements StrongSectorService {
    @Override
    public void syncStrongSector(String tradeDate) {
        TradingRes tradingRes = TuShareApi.post("limit_cpt_list", tradeDate);
        List<StrongSector> stockList = tradingRes.parse(StrongSector.class);
        for (StrongSector stock : stockList) {
            String upStat = stock.getUpStat().replaceAll("板", "");
            String[] result = upStat.split("天");
            StrongSector strongSector = lambdaQuery()
                    .eq(StrongSector::getTsCode, stock.getTsCode())
                    .eq(StrongSector::getTradeDate, stock.getTradeDate()).one();
            if("首".equals(result[0])){
                stock.setDay(1);
                stock.setBan(1);
            }else{
                stock.setDay(Integer.valueOf(result[0]));
                stock.setBan(Integer.valueOf(result[1]));
            }

            if(strongSector == null){
                save(stock) ;
            }else{
                stock.setId(strongSector.getId());
                updateById(stock) ;
            }
        }
    }
    // 可在此实现自定义业务逻辑方法

}
